package com.bdjk.bdmw.InfoGath.communication.protocol.iparent;


import java.text.SimpleDateFormat;
import java.util.Date;

import com.bdjk.bdmw.InfoGath.business.iparent.vo.IparentPositionVO;
import com.bdjk.bdmw.InfoGath.communication.protocol.BaseProtocol;

public class IParentGPSProtocol extends BaseProtocol<IparentPositionVO>{
	
	public IParentGPSProtocol(){
		this.isResponse=false;
		this.isSave=true;
	}


	@Override
	public String encodeMessage(IparentPositionVO model) {
		// TODO Auto-generated method stub
		return null;
	}
 
	@Override
	public IparentPositionVO decodeMessage(String message) {
		// TODO Auto-generated method stub
		String[] strs=message.split(",");
		IparentPositionVO vo=new IparentPositionVO();
		vo.setVersion(strs[0]);
		vo.setEncryptType(strs[2]);
		vo.setValidatCode(strs[3]);
		vo.setSendTime(strs[4]);
		vo.setTerType(strs[5]);
		vo.setTerIdent(strs[6]);
		vo.setTimeZone(strs[7]);
		vo.setMesType(strs[8]);
		vo.setDataType(strs[9]);
		vo.setLongitude(strs[10]);
		vo.setLatitude(strs[11]);
		vo.setBaseInfo(strs[12]);
		vo.setAltitude(strs[13]);
		vo.setSpeed(strs[14]);
		vo.setDirection(strs[15]);
		
		//格式化采集时间
		//Calendar calentdar=Calendar.getInstance();
		//calentdar.setTimeInMillis(Long.valueOf(strs[16]));
		//Date date=calentdar.getTime();
		String pattern="yyyy-MM-dd hh:mm:ss";
		SimpleDateFormat format=new SimpleDateFormat(pattern);
		Date date=new Date(Long.valueOf(strs[16]));
		vo.setCollectMills(format.format(date));
		//System.out.println("after: "+format.format(date));
		this.terIdent=vo.getTerIdent();
		return vo;
	}

	
	/*
	@Override
	public boolean verify(String message) {
		// TODO Auto-generated method stub
		//message="HM*8800050015*0087*UD,220414,134652,A,22.571707,N,113.8613968,E,0.1,0.0,100,7,60,90,1000,50,0000,4,1,460,0,9360,4082,131,9360,4092,148,9360,4091,143,9360,4153,141";
		Pattern p;
		Matcher m;
		boolean label;
				
		//检验设备号是否达标,默认设备号是10位,且10位都是数字
		p=Pattern.compile("HM\\*[0-9]{10}\\*[0-9A-Fa-f]*\\*(UD|LOWBAT|MONITOR),.*");
		m=p.matcher(message);
		label=m.matches();
		if(!label){
			System.out.println("输入的协议不符合规则");
				return label;
		}
				
		//检验长度是否达标
		String prefixstr="";
		String lenstr="";
		String suffixstr="";
		p=Pattern.compile("HM\\*[0-9]{10}\\*[0-9A-Fa-f]{4}\\*");
		m=p.matcher(message);while(m.find()){
			prefixstr=m.group();
		}
		suffixstr=message.substring(prefixstr.length());
		p=Pattern.compile("\\*[0-9A-Fa-f]{4}\\*");
		m=p.matcher(prefixstr);
		while(m.find()){
			lenstr=m.group();
			lenstr=lenstr.substring(1,lenstr.length()-1);
		}
				//System.out.println("full string: "+message);
				//System.out.println("prefix string: "+prefixstr);
				//System.out.println("len string: "+lenstr);
				//System.out.println("suffix string: "+suffixstr);
				//int x=message.length()-prefixstr.length();
				//System.out.println("length:"+Integer.parseInt(lenstr,16)+" ..... "+suffixstr.length()+"  .....  "+x);
		if(message.length()-prefixstr.length()!=Integer.parseInt(lenstr)){
			label=false;
			System.out.println("输入的协议长度不合法");
		}
		return label;
	}*/

}
